#include "mapFileReader.h"

MapFileReader::MapFileReader( const char* mapFileIn ) : mFile( mapFileIn )
{
	parseText();
	mArray = new std::string*[MAX_MAP_SIZE];
	for( int i = 0; i < MAX_MAP_SIZE; i++ )
	{
		mArray[i] = new std::string[MAX_MAP_SIZE];
	}
	parseTextLines();
}

MapFileReader::~MapFileReader()
{
	for( int i = 0; i < MAX_MAP_SIZE; i++ )
	{
		delete [] mArray[i];
	}
}

void MapFileReader::parseText()
{
	std::string line;
	std::ifstream infile( mFile );
	if ( infile.is_open() )
	{
		while ( getline( infile, line ) )
		{
			mVec.push_back( line.c_str() );
		}
		infile.close();
	}
}

void MapFileReader::parseTextLines()
{
	std::vector<std::string> sVec;
	for( unsigned int i = 0; i < mVec.size(); ++i )
	{
		std::string::iterator b = mVec[i].begin(), e = mVec[i].end();
		while( b != e )
		{
			std::string blockCode;
			while( b != e && *b != ' ' )
			{
				blockCode.push_back( *b );
				++b;
			}
			sVec.push_back( blockCode );
			//step pass the blank spot
			if( b != e)
				++b;
			else if( i != 0 )
				// *0* is the end of line escape character
				sVec.push_back("*0*");
		}
		// checks the first line to get the images to be loaded.
		// then clears sVec so that it can store the rest of the
		// map info.
		if( i == 0 )
		{
			tVec = sVec;
			std::vector<std::string> clearVec;
			sVec = clearVec;
		}

	}
	//changes the vector of strings into a double array 
	//of strings
	int width = 0, height = 0;
	for( unsigned int i = 0; i < sVec.size(); i++ )
	{
		if( strcmp( sVec[i].c_str(), isNewLine() ) == 0 )
		{
			height++;
			width = 0;
		}
		else
		{
			mArray[height][width] = sVec[i].c_str();
			width++;
		}
	}
	mVec = sVec;
}